const cheerio = require('cheerio')
const LRU = require('lru-cache')
module.exports = {
  env: {
    baseUrl: process.env.BASE_URL || 'http://localhost:3000',
    debug: Boolean(process.env.DEBUG) || false,
    cache: Boolean(process.env.CACHE) || false,
    cacheTime: (Number(process.env.CACHE_TIME) || 20) * 10e2,
    cacheMax: Number(process.env.CACHE_TIME) || 20
  },
  head: {
    title: '第一个Nuxtjs网站',
    titleTemplate: (titleChunk) => {
      return titleChunk ? `${titleChunk}-第一个Nuxtjs网站` : '第一个Nuxtjs网站'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'keywrods', name: 'keywrods', content: 'Nuxt.js project' },
      { hid: 'description', name: 'description', content: 'Nuxt.js project' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },
  plugins: [
    { src: '~plugins/element-ui', ssr: true },
    '~/plugins/axios-cache-adapter',
    '~/plugins/axios',
    '~/plugins/api',
  ],
  css: [
    'element-ui/lib/theme-chalk/index.css'
  ],
  modules: [
    'cookie-universal-nuxt',
    '@nuxtjs/axios',
    ['@nuxtjs/component-cache', { maxAge: 1000 * 60 * 15, max: 1000 }],
    ['@nuxtjs/dotenv', { filename: process.env.MODE ? '.env.prod' : '.env' }]
  ],
  server: {
    port: process.env.PORT || 3000, // default: 3000
    host: process.env.HOST || 'localhost',
  },
  axios: {
    proxy: process.env.PROXY || false
  },
  proxy: {
    '/api': {
      target: process.env.PROXY_TARGET || '',
      changeOrigin: process.env.PROXY_ORIGIN || false,
      pathRewrite: {
        '^/api': process.env.PROXY_REWRITE || '',
      }
    }
  },
  /*
  ** Customize the progress bar color
  */
  loading: {
    color: '#3B8070',
    continuous: true
  },
  // loading: '~/components/loading',
  /*
  ** Build configuration
  */
  build: {
    vendor: ['axios', 'element-ui'],
    // transpile: [/element-ui/],
    loaders: {
      imgUrl: { limit: 1000 }
    },
    /*
    ** Run ESLint on save
    */
    extend (config, { isDev, isClient }) {
      if (isDev && isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    },
    publicPath: '/.nuxt/dist/client/',
    minimize: true,
    optimizeCSS: true,
    extractCSS: true,
    html:{
      minify: {
        collapseBooleanAttributes: true,
        collapseWhitespace: true,
        decodeEntities: false,
        minifyCSS: true,
        minifyJS: true,
      }
    }
  },
  hooks: {
    'vue-renderer:ssr:context'(context) {
      const routePath = JSON.stringify(context.nuxt.routePath)
      context.nuxt = { serverRendered: true, routePath }
    },
    'render:route': (url, result) => {
      this.$ = cheerio.load(result.html,{decodeEntities: false})
      this.$(`body script`).eq(0).remove()
      result.html = this.$.html()
    }
  },
  // render: {
  //   bundleRenderer: {
  //    cache: new LRU({
  //     max: process.env.cacheMax,
  //     maxAge: process.env.cacheTime
  //    })
  //   }
  // },
  serverMiddleware: ['~/page-cache.js']
}
